package demo2;

import org.junit.Test;

import java.util.Arrays;
import java.util.Optional;
import java.util.OptionalInt;

public class TestOptional {

    public static void main(String[] args) {

      //  String cardNo = student.getCard().getCardNo().toUpperCase();

        //String cardNoUpper = null;
//        if(student != null){
//            Card card = student.getCard();
//            if(card != null){
//                String cardNo = card.getCardNo();
//                if (cardNo != null){
//                    cardNoUpper = cardNo.toUpperCase();
//                }
//            }
//        }
        Student student = new Student("新三", 18, 55.0);
        Card cardObj = new Card("ACafgtc", "工商银行");
        student.setCard(cardObj);
        Optional<Student> o = Optional.ofNullable(null);
       // Optional<Student> o = Optional.ofNullable(student);
        String cardNoUpper = o
                .flatMap(s -> s.getCard())   // 获得 Optional<Card>
                .map(card -> card.getCardNo())    // 获得 Card 内的 cardNo
                .map(cardNo -> cardNo.toUpperCase())
                .orElse("默认值");
        System.out.println(cardNoUpper);

    }

    //`Optional.of(T t)` : 创建一个 Optional 实例，t必须非空
    @Test
    public void test1(){
//        Optional<Student> student = Optional.of(new Student());
//        Student student1 = student.get();
//        System.out.println(student1);

        Optional<Student> student2 = Optional.of(null);
    }

    //` Optional.empty() `: 创建一个空的 Optional 实例
    @Test
    public void test2(){
        Optional<Student> student = Optional.empty();
        Student student1 = student.get();
        System.out.println(student1);
    }

    //`Optional.ofNullable(T t)`：t 可以为null
    @Test
    public void test3(){
        Optional<Student> student = Optional.ofNullable(null);
        Student student1 = student.get();
        System.out.println(student1);
    }

    //`boolean isPresent()·`: 判断是否包含值
    @Test
    public void test4(){
        Optional<Student> student = Optional.ofNullable(new Student());
        if(student.isPresent()){
            Student student1 = student.get();
            System.out.println(student1);
        }
        Optional<Student> student2 = Optional.ofNullable(null);
        if (!student2.isPresent()) {
            System.out.println("该对象为空值");
        }
    }
    @Test
    public void test5(){
        Optional<Student> student = Optional.ofNullable(new Student("新三", 18, 55.0));
        student.ifPresent((x)-> {
            x.setName("李四");
        });
        System.out.println(student.get());
    }

    //`T orElse(T other)` ：如果有值则将其返回，否则返回指定的other对象
    @Test
    public void test6(){
        Optional<Student> op = Optional.ofNullable(null);
        Student st = op.orElse(new Student("新三", 18, 55.0));
        System.out.println(st);

        Optional<Student> op2 = Optional.ofNullable(new Student());
        Student st2 = op2.orElse(new Student("新三", 18, 55.0));
        System.out.println(st2);
    }

    //`T orElseGet(Supplier<? extends T> other)` ：如果有值则将其返回，否则返回由Supplier接口实现提供的对象。
    @Test
    public void test7(){
        Optional<Student> op = Optional.ofNullable(null);
        Student st = op.orElseGet(()->{
            //由于是一个函数式接口，里面可以写逻辑
            Student student = new Student();
            student.setAge(99);
            student.setGrades(888.2);
            student.setName("李四");
            return student;
        });
        System.out.println(st);
    }

    @Test
    public void test8(){
        Optional<Student> op = Optional.ofNullable(null);
        Student st = op.orElseThrow(() -> new RuntimeException("值不存在"));
        System.out.println(st);
    }

    @Test
    public void test9(){
        Optional<Student> op = Optional.ofNullable(new Student("新,三", 18, 55.0));
        Optional<String[]> s1 = op.map(s -> s.getName())
                .map(s -> s.split(","));
        System.out.println(Arrays.toString(s1.get()));
    }

    @Test
    public void test10(){
        Optional<Student> op = Optional.ofNullable(new Student("新,三", 18, 55.0));
        Optional<String[]> s1 = op.flatMap(s -> Optional.ofNullable(s.getName())
                                  .flatMap(s2 -> Optional.ofNullable(s2.split(",")))
        );
        System.out.println(Arrays.toString(s1.get()));
    }

    @Test
    public void test11(){
        Optional<Student> optional = Optional.of(new Student("新三", 18, 55.0));
        Optional<Student> opt = optional.filter(s -> {
            return "张".equals(s.getName());
        });
        System.out.println(opt.orElse(new Student("李四", 22, 223.0)));
    }




}
